import { Injectable } from '@angular/core';
import {
    CanActivate,
    ActivatedRouteSnapshot,
    RouterStateSnapshot
} from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { UserService } from './services/user.service';
import { Router } from '@angular/router';

/// 是否有跳转到做题权限
@Injectable()
export class CanDeactivateGuardService implements CanActivate {
    constructor(private userSrv: UserService, private router: Router) {}

    canActivate(
        route: ActivatedRouteSnapshot,
        state: RouterStateSnapshot
    ): Observable<boolean> | Promise<boolean> | boolean {
        return new Observable(observer => {
            if (this.userSrv.getUser() != null) {
                observer.next(true);
                observer.complete();
                return;
            }

            console.log('授权不足');
            this.router.navigate(['/login']);
            observer.next(false);
            observer.complete();
        });
    }
}
